{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "ename": "OSError",
     "evalue": "dlopen(/Users/yphacker/opt/anaconda3/envs/py36/lib/python3.6/site-packages/lightgbm/lib_lightgbm.so, 6): Symbol not found: ___emutls_get_address\n  Referenced from: /usr/local/opt/gcc@8/lib/gcc/8/libstdc++.6.dylib\n  Expected in: /usr/lib/libSystem.B.dylib\n in /usr/local/opt/gcc@8/lib/gcc/8/libstdc++.6.dylib",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mOSError\u001b[0m                                   Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-1-5dacb4a27011>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mimport\u001b[0m \u001b[0mlightgbm\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0mlgb\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;32m~/opt/anaconda3/envs/py36/lib/python3.6/site-packages/lightgbm/__init__.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m      6\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0m__future__\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mabsolute_import\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      7\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 8\u001b[0;31m \u001b[0;32mfrom\u001b[0m \u001b[0;34m.\u001b[0m\u001b[0mbasic\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mBooster\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mDataset\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      9\u001b[0m from .callback import (early_stopping, print_evaluation, record_evaluation,\n\u001b[1;32m     10\u001b[0m                        reset_parameter)\n",
      "\u001b[0;32m~/opt/anaconda3/envs/py36/lib/python3.6/site-packages/lightgbm/basic.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m     32\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     33\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 34\u001b[0;31m \u001b[0m_LIB\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0m_load_lib\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     35\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     36\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/opt/anaconda3/envs/py36/lib/python3.6/site-packages/lightgbm/basic.py\u001b[0m in \u001b[0;36m_load_lib\u001b[0;34m()\u001b[0m\n\u001b[1;32m     27\u001b[0m     \u001b[0;32mif\u001b[0m \u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlib_path\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     28\u001b[0m         \u001b[0;32mreturn\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 29\u001b[0;31m     \u001b[0mlib\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mctypes\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcdll\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mLoadLibrary\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlib_path\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     30\u001b[0m     \u001b[0mlib\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mLGBM_GetLastError\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrestype\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mctypes\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mc_char_p\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     31\u001b[0m     \u001b[0;32mreturn\u001b[0m \u001b[0mlib\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/opt/anaconda3/envs/py36/lib/python3.6/ctypes/__init__.py\u001b[0m in \u001b[0;36mLoadLibrary\u001b[0;34m(self, name)\u001b[0m\n\u001b[1;32m    424\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    425\u001b[0m     \u001b[0;32mdef\u001b[0m \u001b[0mLoadLibrary\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mname\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 426\u001b[0;31m         \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_dlltype\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mname\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    427\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    428\u001b[0m \u001b[0mcdll\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mLibraryLoader\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mCDLL\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/opt/anaconda3/envs/py36/lib/python3.6/ctypes/__init__.py\u001b[0m in \u001b[0;36m__init__\u001b[0;34m(self, name, mode, handle, use_errno, use_last_error)\u001b[0m\n\u001b[1;32m    346\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    347\u001b[0m         \u001b[0;32mif\u001b[0m \u001b[0mhandle\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 348\u001b[0;31m             \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_handle\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0m_dlopen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_name\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmode\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    349\u001b[0m         \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    350\u001b[0m             \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_handle\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mhandle\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mOSError\u001b[0m: dlopen(/Users/yphacker/opt/anaconda3/envs/py36/lib/python3.6/site-packages/lightgbm/lib_lightgbm.so, 6): Symbol not found: ___emutls_get_address\n  Referenced from: /usr/local/opt/gcc@8/lib/gcc/8/libstdc++.6.dylib\n  Expected in: /usr/lib/libSystem.B.dylib\n in /usr/local/opt/gcc@8/lib/gcc/8/libstdc++.6.dylib"
     ]
    }
   ],
   "source": [
    "import lightgbm as lgb"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/yphacker/opt/anaconda3/envs/py36/lib/python3.6/site-packages/ipykernel_launcher.py:2: TqdmExperimentalWarning: Using `tqdm.autonotebook.tqdm` in notebook mode. Use `tqdm.tqdm` instead to force console mode (e.g. in jupyter console)\n",
      "  \n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "8bbf9988c1dc46ae97e74d8c41b4c5b0",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "HBox(children=(FloatProgress(value=0.0, max=17880.0), HTML(value='')))"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "ff2f71e149aa4e21a868baef42c7f441",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "HBox(children=(FloatProgress(value=0.0, max=17880.0), HTML(value='')))"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "05d8ce89046c46faac891b12c03bf28d",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "HBox(children=(FloatProgress(value=0.0, max=17880.0), HTML(value='')))"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "开始计算tf-idf特征\n",
      "计算结束\n",
      "开始进行一些前期处理\n",
      "处理完毕\n",
      "\n",
      "****开始跑 LogisticRegression ****\n",
      "LogisticRegression 处理完毕\n",
      "五折结果 [0.981, 0.97965, 0.98145, 0.97919, 0.98009, 0.98778, 0.98325, 0.98008]\n",
      "平均结果 0.9815612499999999\n",
      "\n",
      "****开始跑 SGDClassifier ****\n",
      "SGDClassifier 处理完毕\n",
      "五折结果 [0.96653, 0.96653, 0.96742, 0.96923, 0.96652, 0.97014, 0.96786, 0.96831]\n",
      "平均结果 0.9678175\n",
      "\n",
      "****开始跑 PassiveAggressiveClassifier ****\n",
      "PassiveAggressiveClassifier 处理完毕\n",
      "五折结果 [0.99186, 0.98869, 0.98959, 0.98824, 0.98824, 0.99412, 0.9914, 0.99049]\n",
      "平均结果 0.99032875\n",
      "\n",
      "****开始跑 RidgeClassfiy ****\n",
      "RidgeClassfiy 处理完毕\n",
      "五折结果 [0.98507, 0.98417, 0.98462, 0.98371, 0.98371, 0.9914, 0.98551, 0.98551]\n",
      "平均结果 0.9854625\n",
      "\n",
      "****开始跑 LinearSVC ****\n",
      "LinearSVC 处理完毕\n",
      "五折结果 [0.98869, 0.98779, 0.98733, 0.98507, 0.98643, 0.99231, 0.98914, 0.98778]\n",
      "平均结果 0.9880675\n",
      "特征处理完毕......\n"
     ]
    },
    {
     "ename": "OSError",
     "evalue": "dlopen(/Users/yphacker/opt/anaconda3/envs/py36/lib/python3.6/site-packages/lightgbm/lib_lightgbm.so, 6): Library not loaded: /usr/local/opt/gcc/lib/gcc/8/libgomp.1.dylib\n  Referenced from: /Users/yphacker/opt/anaconda3/envs/py36/lib/python3.6/site-packages/lightgbm/lib_lightgbm.so\n  Reason: image not found",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mOSError\u001b[0m                                   Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-1-c478ce1114fa>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m    156\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    157\u001b[0m \u001b[0;31m###################### lgb ##########################\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 158\u001b[0;31m \u001b[0;32mimport\u001b[0m \u001b[0mlightgbm\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0mlgb\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    159\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    160\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'载入数据......'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/opt/anaconda3/envs/py36/lib/python3.6/site-packages/lightgbm/__init__.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m      6\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0m__future__\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mabsolute_import\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      7\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 8\u001b[0;31m \u001b[0;32mfrom\u001b[0m \u001b[0;34m.\u001b[0m\u001b[0mbasic\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mBooster\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mDataset\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      9\u001b[0m from .callback import (early_stopping, print_evaluation, record_evaluation,\n\u001b[1;32m     10\u001b[0m                        reset_parameter)\n",
      "\u001b[0;32m~/opt/anaconda3/envs/py36/lib/python3.6/site-packages/lightgbm/basic.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m     33\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     34\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 35\u001b[0;31m \u001b[0m_LIB\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0m_load_lib\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     36\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     37\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/opt/anaconda3/envs/py36/lib/python3.6/site-packages/lightgbm/basic.py\u001b[0m in \u001b[0;36m_load_lib\u001b[0;34m()\u001b[0m\n\u001b[1;32m     28\u001b[0m     \u001b[0;32mif\u001b[0m \u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlib_path\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     29\u001b[0m         \u001b[0;32mreturn\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 30\u001b[0;31m     \u001b[0mlib\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mctypes\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcdll\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mLoadLibrary\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlib_path\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     31\u001b[0m     \u001b[0mlib\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mLGBM_GetLastError\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrestype\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mctypes\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mc_char_p\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     32\u001b[0m     \u001b[0;32mreturn\u001b[0m \u001b[0mlib\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/opt/anaconda3/envs/py36/lib/python3.6/ctypes/__init__.py\u001b[0m in \u001b[0;36mLoadLibrary\u001b[0;34m(self, name)\u001b[0m\n\u001b[1;32m    424\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    425\u001b[0m     \u001b[0;32mdef\u001b[0m \u001b[0mLoadLibrary\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mname\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 426\u001b[0;31m         \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_dlltype\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mname\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    427\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    428\u001b[0m \u001b[0mcdll\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mLibraryLoader\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mCDLL\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/opt/anaconda3/envs/py36/lib/python3.6/ctypes/__init__.py\u001b[0m in \u001b[0;36m__init__\u001b[0;34m(self, name, mode, handle, use_errno, use_last_error)\u001b[0m\n\u001b[1;32m    346\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    347\u001b[0m         \u001b[0;32mif\u001b[0m \u001b[0mhandle\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 348\u001b[0;31m             \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_handle\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0m_dlopen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_name\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmode\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    349\u001b[0m         \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    350\u001b[0m             \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_handle\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mhandle\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mOSError\u001b[0m: dlopen(/Users/yphacker/opt/anaconda3/envs/py36/lib/python3.6/site-packages/lightgbm/lib_lightgbm.so, 6): Library not loaded: /usr/local/opt/gcc/lib/gcc/8/libgomp.1.dylib\n  Referenced from: /Users/yphacker/opt/anaconda3/envs/py36/lib/python3.6/site-packages/lightgbm/lib_lightgbm.so\n  Reason: image not found"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "from tqdm.autonotebook import *\n",
    "from bs4 import BeautifulSoup\n",
    "import re\n",
    "\n",
    "tqdm.pandas()\n",
    "\n",
    "train = pd.read_csv('../data/train.csv')\n",
    "test = pd.read_csv('../data/test.csv')\n",
    "\n",
    "data = pd.concat([train, test], axis=0, sort=False).reset_index(drop=True)\n",
    "data = data.fillna(-1)\n",
    "def salary_range_min(row):\n",
    "    try:\n",
    "        result = int(str(row['salary_range']).split('-')[0])\n",
    "    except Exception:\n",
    "        result = -1\n",
    "    return result\n",
    "\n",
    "def salary_range_max(row):\n",
    "    try:\n",
    "        result = int(str(row['salary_range']).split('-')[1])\n",
    "    except Exception:\n",
    "        result = -1\n",
    "    return result\n",
    "\n",
    "def location_2(row):\n",
    "    try:\n",
    "        result = str(row).split(',')[1]\n",
    "    except Exception:\n",
    "        result = '未知'\n",
    "    return result\n",
    "\n",
    "normal_feature = pd.DataFrame()\n",
    "normal_feature['salary_min'] = data.progress_apply(lambda row:salary_range_min(row), axis=1)\n",
    "normal_feature['salary_max'] = data.progress_apply(lambda row:salary_range_max(row), axis=1)\n",
    "normal_feature['salary_median'] = (normal_feature['salary_max'] + normal_feature['salary_min'])/2\n",
    "normal_feature['salary_range'] = normal_feature['salary_max'] - normal_feature['salary_min']\n",
    "normal_feature['telecommuting'] = list(data['telecommuting'])\n",
    "normal_feature['has_company_logo'] = list(data['has_company_logo'])\n",
    "normal_feature['has_questions'] = list(data['has_questions'])\n",
    "from sklearn.preprocessing import LabelEncoder\n",
    "labelencoder = LabelEncoder()\n",
    "normal_feature['employment_type'] = labelencoder.fit_transform(data['employment_type'].astype(str))\n",
    "normal_feature['required_experience'] = labelencoder.fit_transform(data['required_experience'].astype(str))\n",
    "normal_feature['required_education'] = labelencoder.fit_transform(data['required_education'].astype(str))\n",
    "normal_feature['industry'] = labelencoder.fit_transform(data['industry'].astype(str))\n",
    "normal_feature['function'] = labelencoder.fit_transform(data['function'].astype(str))\n",
    "\n",
    "data['review'] = data.progress_apply(lambda row:str(row['title']) + ' ' + str(row['location']) + ' ' + str(row['company_profile']) + ' ' + \n",
    "                                   str(row['description']) + ' ' + str(row['department']) + ' ' + str(row['requirements']) + ' ' + str(row['benefits']), axis=1)\n",
    "\n",
    "import warnings\n",
    "warnings.filterwarnings(\"ignore\")\n",
    "\n",
    "import pandas as pd\n",
    "from sklearn.feature_extraction.text import TfidfVectorizer\n",
    "import jieba\n",
    "from tqdm import *\n",
    "from sklearn.model_selection import StratifiedKFold\n",
    "import numpy as np\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "from sklearn.linear_model import SGDClassifier\n",
    "from sklearn.linear_model import PassiveAggressiveClassifier\n",
    "from sklearn.linear_model import RidgeClassifier\n",
    "from sklearn.naive_bayes import BernoulliNB\n",
    "from sklearn.naive_bayes import MultinomialNB\n",
    "from sklearn.svm import LinearSVC\n",
    "from sklearn.metrics import accuracy_score\n",
    "\n",
    "df_train = data[:len(train)]\n",
    "df_test = data[len(train):]\n",
    "\n",
    "df_train['label'] = df_train['fraudulent'].astype(int)\n",
    "data = pd.concat([df_train, df_test], axis=0, sort=False)\n",
    "data['review'] = data['review'].apply(lambda row:str(row))\n",
    "\n",
    "############################ tf-idf ############################\n",
    "print('开始计算tf-idf特征')\n",
    "tf = TfidfVectorizer(ngram_range=(1, 2), min_df=3, max_df=0.9, use_idf=1, smooth_idf=1, sublinear_tf=1)\n",
    "discuss_tf = tf.fit_transform(data['review']).tocsr()\n",
    "print('计算结束')\n",
    "\n",
    "############################ 切分数据集 ##########################\n",
    "print('开始进行一些前期处理')\n",
    "train_feature = discuss_tf[:len(df_train)]\n",
    "score = df_train['label']\n",
    "test_feature = discuss_tf[len(df_train):]\n",
    "print('处理完毕')\n",
    "\n",
    "######################### 模型函数(返回sklean_stacking结果) ########################\n",
    "def get_sklearn_classfiy_stacking(clf, train_feature, test_feature, score, model_name, class_number, n_folds, train_num, test_num):\n",
    "    print('\\n****开始跑', model_name, '****')\n",
    "    stack_train = np.zeros((train_num, class_number))\n",
    "    stack_test = np.zeros((test_num, class_number))\n",
    "    score_mean = []\n",
    "    skf = StratifiedKFold(n_splits=n_folds, random_state=1017)\n",
    "    tqdm.desc = model_name\n",
    "    for i, (tr, va) in enumerate(skf.split(train_feature, score)):\n",
    "        clf.fit(train_feature[tr], score[tr])\n",
    "        score_va = clf._predict_proba_lr(train_feature[va])\n",
    "        score_te = clf._predict_proba_lr(test_feature)\n",
    "        score_single = accuracy_score(score[va], clf.predict(train_feature[va]))\n",
    "        score_mean.append(np.around(score_single, 5))\n",
    "        stack_train[va] += score_va\n",
    "        stack_test += score_te\n",
    "    stack_test /= n_folds\n",
    "    stack = np.vstack([stack_train, stack_test])\n",
    "    df_stack = pd.DataFrame()\n",
    "    df_stack['tfidf_' + model_name + '_classfiy_{}'.format(1)] = stack[:, 1]\n",
    "    print(model_name, '处理完毕')\n",
    "    return df_stack, score_mean\n",
    "\n",
    "model_list = [\n",
    "    ['LogisticRegression', LogisticRegression(random_state=1017, C=3)],\n",
    "    ['SGDClassifier', SGDClassifier(random_state=1017, loss='log')],\n",
    "    ['PassiveAggressiveClassifier', PassiveAggressiveClassifier(random_state=1017, C=2)],\n",
    "    ['RidgeClassfiy', RidgeClassifier(random_state=1017)],\n",
    "    ['LinearSVC', LinearSVC(random_state=1017)]\n",
    "]\n",
    "\n",
    "stack_feature = pd.DataFrame()\n",
    "for i in model_list:\n",
    "    stack_result, score_mean = get_sklearn_classfiy_stacking(i[1], train_feature, test_feature, score, i[0], 2, 8, len(df_train), len(df_test))\n",
    "    stack_feature = pd.concat([stack_feature, stack_result], axis=1, sort=False)\n",
    "    print('五折结果', score_mean)\n",
    "    print('平均结果', np.mean(score_mean))\n",
    "normal_feature = pd.concat([stack_feature, normal_feature], axis=1, sort=False)\n",
    "\n",
    "import pandas as pd\n",
    "\n",
    "train = pd.read_csv('../data/train.csv')\n",
    "test = pd.read_csv('../data/test.csv')\n",
    "\n",
    "# f1 = pd.read_csv('feature/normal_feature.csv')\n",
    "# f2 = pd.read_csv('feature/w2v_feature.csv')\n",
    "# f3 = pd.read_csv('feature/w2v_extend_feature.csv')\n",
    "\n",
    "df_feature = normal_feature\n",
    "\n",
    "train_feature = df_feature[:len(train)]\n",
    "test_feature = df_feature[len(train):]\n",
    "\n",
    "label = train['fraudulent'].astype(int)\n",
    "\n",
    "import pandas as pd\n",
    "from sklearn import model_selection\n",
    "import numpy as np\n",
    "from sklearn.metrics import mean_squared_error, accuracy_score\n",
    "X_train, X_test, Y_train, Y_test = model_selection.train_test_split(train_feature, label, test_size=0.2,random_state=1017)\n",
    "# train_feature = X_train\n",
    "# label = Y_train\n",
    "\n",
    "print('特征处理完毕......')\n",
    "\n",
    "\n",
    "###################### lgb ##########################\n",
    "import lightgbm as lgb\n",
    "\n",
    "print('载入数据......')\n",
    "lgb_train = lgb.Dataset(train_feature, label)\n",
    "lgb_eval = lgb.Dataset(X_test, Y_test, reference=lgb_train)\n",
    "\n",
    "\n",
    "print('开始训练......')\n",
    "params = {\n",
    "            'boosting_type': 'gbdt',\n",
    "            'learning_rate' : 0.01, \n",
    "            'verbose': 0,\n",
    "#             'metrics':{'binary_error'},\n",
    "#             'num_leaves':32,\n",
    "            'objective':'binary',\n",
    "#             'feature_fraction': 0.2,\n",
    "#             'bagging_fraction':0.7 ,\n",
    "            'seed': 1024,\n",
    "            'nthread': 50,\n",
    "        }\n",
    "\n",
    "gbm = lgb.train(params,\n",
    "                lgb_train,\n",
    "                num_boost_round=1000,\n",
    "                valid_sets=lgb_eval,\n",
    "                verbose_eval=20,\n",
    "                )\n",
    "\n",
    "temp = gbm.predict(X_test)\n",
    "\n",
    "\n",
    "print('结果：' + str(1/(1+mean_squared_error(Y_test, temp))))\n",
    "print('特征重要性：'+ str(list(gbm.feature_importance())))\n",
    "\n",
    "y_test = gbm.predict(test_feature)\n",
    "test_change_label = y_test.copy()\n",
    "\n",
    "y_test_pos = np.argsort(y_test)\n",
    "\n",
    "test_change_label[y_test_pos[:100]] = 0\n",
    "test_change_label[y_test_pos[100:]] = 1\n",
    "result = pd.DataFrame()\n",
    "result['id'] = np.arange(0, len(y_test), 1)\n",
    "result['result'] = np.around(test_change_label)\n",
    "result['result'] = result['result'].astype(int)\n",
    "result.to_csv(f'result/lgb.csv', index=False, header=None)\n",
    "result.result.value_counts()\n",
    "\n",
    "test_1 = test_change_label.copy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "\n",
    "train = pd.read_csv('../data/train.csv')\n",
    "test = pd.read_csv('../data/test.csv')\n",
    "\n",
    "f1 = pd.read_csv('feature/normal_feature.csv')\n",
    "f2 = pd.read_csv('feature/w2v_feature.csv')\n",
    "\n",
    "df_feature = pd.concat([f1, f2], axis=1, sort=False)\n",
    "\n",
    "\n",
    "\n",
    "train_feature = df_feature[:len(train)]\n",
    "test_feature = df_feature[len(train):]\n",
    "\n",
    "label = train['fraudulent'].astype(int)\n",
    "\n",
    "\n",
    "import pandas as pd\n",
    "from sklearn import model_selection\n",
    "import numpy as np\n",
    "from sklearn.metrics import mean_squared_error, accuracy_score\n",
    "X_train, X_test, Y_train, Y_test = model_selection.train_test_split(train_feature, label, test_size=0.2,random_state=1017)\n",
    "# train_feature = X_train\n",
    "# label = Y_train\n",
    "\n",
    "print('特征处理完毕......')\n",
    "\n",
    "\n",
    "###################### lgb ##########################\n",
    "import lightgbm as lgb\n",
    "\n",
    "print('载入数据......')\n",
    "lgb_train = lgb.Dataset(train_feature, label)\n",
    "lgb_eval = lgb.Dataset(X_test, Y_test, reference=lgb_train)\n",
    "\n",
    "\n",
    "print('开始训练......')\n",
    "params = {\n",
    "            'boosting_type': 'gbdt',\n",
    "            'learning_rate' : 0.01, \n",
    "            'verbose': 0,\n",
    "#             'metrics':{'binary_error'},\n",
    "#             'num_leaves':32,\n",
    "            'objective':'binary',\n",
    "#             'feature_fraction': 0.2,\n",
    "#             'bagging_fraction':0.7 ,\n",
    "            'seed': 1024,\n",
    "            'nthread': 50,\n",
    "        }\n",
    "\n",
    "gbm = lgb.train(params,\n",
    "                lgb_train,\n",
    "                num_boost_round=110,\n",
    "                valid_sets=lgb_eval,\n",
    "                verbose_eval=20,\n",
    "                )\n",
    "\n",
    "temp = gbm.predict(X_test)\n",
    "\n",
    "\n",
    "print('结果：' + str(1/(1+mean_squared_error(Y_test, temp))))\n",
    "print('特征重要性：'+ str(list(gbm.feature_importance())))\n",
    "\n",
    "y_test = gbm.predict(test_feature)\n",
    "test_change_label = y_test.copy()\n",
    "\n",
    "y_test_pos = np.argsort(y_test)\n",
    "\n",
    "test_change_label[y_test_pos[:100]] = 0\n",
    "test_change_label[y_test_pos[100:]] = 1\n",
    "result = pd.DataFrame()\n",
    "result['id'] = np.arange(0, len(y_test), 1)\n",
    "result['result'] = np.around(test_change_label)\n",
    "result['result'] = result['result'].astype(int)\n",
    "result.to_csv(f'result/lgb.csv', index=False, header=None)\n",
    "\n",
    "test_2 = test_change_label.copy()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "\n",
    "train = pd.read_csv('../data/train.csv')\n",
    "test = pd.read_csv('../data/test.csv')\n",
    "\n",
    "f1 = pd.read_csv('feature/normal_feature.csv')\n",
    "f2 = pd.read_csv('feature/w2v_feature.csv')\n",
    "f3 = pd.read_csv('feature/w2v_extend_feature.csv')\n",
    "\n",
    "\n",
    "df_feature = pd.concat([f1, f2, f3], axis=1, sort=False)\n",
    "\n",
    "import random\n",
    "random.seed(1024)\n",
    "a = random.sample(list(df_feature.columns), 40)\n",
    "df_feature = df_feature[a]\n",
    "\n",
    "train_feature = df_feature[:len(train)]\n",
    "test_feature = df_feature[len(train):]\n",
    "\n",
    "\n",
    "label = train['fraudulent'].astype(int)\n",
    "\n",
    "\n",
    "import pandas as pd\n",
    "from sklearn import model_selection\n",
    "import numpy as np\n",
    "from sklearn.metrics import mean_squared_error, accuracy_score\n",
    "X_train, X_test, Y_train, Y_test = model_selection.train_test_split(train_feature, label, test_size=0.2,random_state=1017)\n",
    "# train_feature = X_train\n",
    "# label = Y_train\n",
    "\n",
    "print('特征处理完毕......')\n",
    "\n",
    "\n",
    "###################### lgb ##########################\n",
    "import lightgbm as lgb\n",
    "\n",
    "print('载入数据......')\n",
    "lgb_train = lgb.Dataset(train_feature, label)\n",
    "lgb_eval = lgb.Dataset(X_test, Y_test, reference=lgb_train)\n",
    "\n",
    "\n",
    "print('开始训练......')\n",
    "params = {\n",
    "            'boosting_type': 'gbdt',\n",
    "            'learning_rate' : 0.01, \n",
    "            'verbose': 0,\n",
    "#             'metrics':{'binary_error'},\n",
    "#             'num_leaves':32,\n",
    "            'objective':'binary',\n",
    "#             'feature_fraction': 0.2,\n",
    "#             'bagging_fraction':0.7 ,\n",
    "            'seed': 1024,\n",
    "            'nthread': 50,\n",
    "        }\n",
    "\n",
    "gbm = lgb.train(params,\n",
    "                lgb_train,\n",
    "                num_boost_round=500,\n",
    "                valid_sets=lgb_eval,\n",
    "                verbose_eval=20,\n",
    "                )\n",
    "\n",
    "temp = gbm.predict(X_test)\n",
    "\n",
    "\n",
    "print('结果：' + str(1/(1+mean_squared_error(Y_test, temp))))\n",
    "print('特征重要性：'+ str(list(gbm.feature_importance())))\n",
    "\n",
    "y_test = gbm.predict(test_feature)\n",
    "test_change_label = y_test.copy()\n",
    "\n",
    "y_test_pos = np.argsort(y_test)\n",
    "\n",
    "test_change_label[y_test_pos[:100]] = 0\n",
    "test_change_label[y_test_pos[100:]] = 1\n",
    "result = pd.DataFrame()\n",
    "result['id'] = np.arange(0, len(y_test), 1)\n",
    "result['result'] = np.around(test_change_label)\n",
    "result['result'] = result['result'].astype(int)\n",
    "result.to_csv(f'result/lgb.csv', index=False, header=None)\n",
    "test_3 = test_change_label.copy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_all = (test_1 + test_2 + test_3)/3\n",
    "result['id'] = np.arange(0, len(test_all), 1)\n",
    "result['result'] = np.around(test_all)\n",
    "result['result'] = result['result'].astype(int)\n",
    "result.to_csv(f'result/vote.csv', index=False, header=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
